package tools;
import main.Config;
import graphics.system.Color;
import graphics.system.GLTools;
public class Profiler {
	public static long net = 0, nets = 0;
	public static long graph = 0, graphs = 0;
	public static long phys = 0, physs = 0;
	public static long game = 0, games = 0;
	public static int numLights = 0;
	public static int numPackages;
	public static int sizePackages;
	public static int cnumPackages;
	public static int csizePackages;
	private static long zorders = 0;
	private static long zorder = 0;
	private static long valuesRenew = 0;
	private static long nett, grapht, physt, gamet, zordert;
	public static void startNet() {
		nets = System.currentTimeMillis();
	}
	public static void stopNet() {
		net += System.currentTimeMillis() - nets;
	}
	public static void startGraphics() {
		graphs = System.currentTimeMillis();
	}
	public static void stopGraphics() {
		graph += System.currentTimeMillis() - graphs;
	}
	public static void startPhysics() {
		physs = System.currentTimeMillis();
	}
	public static void stopPhysics() {
		phys += System.currentTimeMillis() - physs;
	}
	public static void startGame() {
		games = System.currentTimeMillis();
	}
	public static void stopGame() {
		game += System.currentTimeMillis() - games;
	}
	public static void startZOrder() {
		zorders = System.currentTimeMillis();
	}
	public static void stopZOrder() {
		zorder += System.currentTimeMillis() - zorders;
	}
	public static long getSumTime() {
		return nett + grapht + physt + gamet;
	}
	public static void feedNumPack() {
	}
	public static void drawProfiler() {
		if (System.currentTimeMillis() - valuesRenew > 1000) {
			valuesRenew = System.currentTimeMillis();
			sizePackages = csizePackages;
			numPackages = cnumPackages;
			csizePackages = 0;
			cnumPackages = 0;
			gamet = game;
			nett = net;
			zordert = zorder;
			physt = phys;
			grapht = graph;
			game = 0;
			net = 0;
			zorder = 0;
			phys = 0;
			graph = 0;
		}
		Point c = new Point(150, 190);
		GLTools.drawRect(c, 300, 280, 0, new Color(0.1f, 0.1f, 0.1f, 0.9f));
		long sum = 1000;// getSumTime();
		long tick = (1000 - getSumTime());
		c.sAdd(new Point(-145, c.y / 2));
		GLTools.drawText(c.x, c.y, "tick:" + tick, Color.white);
		GLTools.drawText(c.x + 200, c.y, "ms  " + (int) (100 * (float) tick / (float) sum) + "%", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Physics:      " + physt, Color.white);
		GLTools.drawText(c.x + 200, c.y, "ms  " + (int) (100 * (float) physt / (float) sum) + "%", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Graphics:     " + grapht, Color.white);
		GLTools.drawText(c.x + 200, c.y, "ms  " + (int) (100 * (float) grapht / (float) sum) + "%", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Network:      " + nett, Color.white);
		GLTools.drawText(c.x + 200, c.y, "ms  " + (int) (100 * (float) nett / (float) sum) + "%", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Game:         " + gamet, Color.white);
		GLTools.drawText(c.x + 200, c.y, "ms  " + (int) (100 * (float) gamet / (float) sum) + "%", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "zOrder:       " + zordert, Color.white);
		GLTools.drawText(c.x + 200, c.y, "ms  " + (int) (100 * (float) zordert / (float) sum) + "%", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Num packages:  " + numPackages, Color.white);
		GLTools.drawText(c.x + 200, c.y, " pack/s", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Size packages: " + (sizePackages / 1024.0f), Color.white);
		GLTools.drawText(c.x + 200, c.y, " kB/s", Color.white);
		c.sAdd(new Point(0, -20));
		Runtime r = Runtime.getRuntime();
		GLTools.drawText(c.x, c.y, "memory:    " + (r.totalMemory() / 1024 / 1024) + " (" + (r.maxMemory() / 1024 / 1024) + ")", Color.white);
		GLTools.drawText(c.x + 200, c.y, " mb", Color.white);
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Num lights:" + numLights, Color.white);
		float pw = Math.max(2, 200 * ((float) physt / (float) sum));
		float gw = Math.max(2, 200 * ((float) grapht / (float) sum));
		float nw = Math.max(2, 200 * ((float) nett / (float) sum));
		float gaw = Math.max(2, 200 * ((float) gamet / (float) sum));
		c.sAdd(new Point(0, -20));
		GLTools.drawText(c.x, c.y, "Phys", Color.green);
		GLTools.drawText(c.x + 60, c.y, "Graph", Color.red);
		GLTools.drawText(c.x + 130, c.y, "Net", Color.blue);
		GLTools.drawText(c.x + 190, c.y, "Game", new Color(1, 1, 0, 1));
		c.sAdd(new Point(pw * 0.5f, -20));
		GLTools.drawRect(c, pw, 10, 0, Color.green);
		c.sAdd(new Point(pw * 0.5f + gw * 0.5f, 0));
		GLTools.drawRect(c, gw, 10, 0, Color.red);
		c.sAdd(new Point(gw * 0.5f + nw * 0.5f, 0));
		GLTools.drawRect(c, nw, 10, 0, Color.blue);
		c.sAdd(new Point(nw * 0.5f + gaw * 0.5f, 0));
		GLTools.drawRect(c, gaw, 10, 0, new Color(1, 1, 0, 1));
	}
}
